//
//  CatType.m
//  CarTown
//
//  Created by xionghao on 15/9/4.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "CatType.h"
#import <MJExtension.h>
#import "AppDelegate.h"

@implementation CatType

@dynamic cat_id;
@dynamic cat_name;

+(void)addCatType:(NSArray *)data{
    AppDelegate *delegate=[[UIApplication sharedApplication]delegate];
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"CatType"];
    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    for (CatType *type in sets) {
        [delegate.managedObjectContext delete:type];
    }
    for (NSDictionary *dict in data) {
        [CatType objectWithKeyValues:dict context:delegate.managedObjectContext];
    }
}
+(NSArray *)getCatType{
     AppDelegate *delegate=[[UIApplication sharedApplication]delegate];
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"CatType"];
    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"cat_id" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sort]];
    
    NSMutableArray *array=[NSMutableArray array];
    for (CatType *type in sets) {
        if (![type.cat_name isEqualToString:@"物流公司"]&&![type.cat_name isEqualToString:@"验车专员"]) {
            [array addObject:type];
        }
    }
    
    return array;
}
+(instancetype)getCatTypeBytext:(NSString *)cat_name{
      AppDelegate *delegate=[[UIApplication sharedApplication]delegate];
     NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"CatType"];
     NSPredicate *pre=[NSPredicate predicateWithFormat:@"cat_name = %@",cat_name];
    request.predicate=pre;
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"cat_id" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sort]];
    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    if (sets.count>0) {
        return  sets[0];
    }else{
        return nil;
    }
    
}
+(instancetype)getCatTypeByid:(NSString *)cat_id{
    AppDelegate *delegate=[[UIApplication sharedApplication]delegate];
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"CatType"];
    NSPredicate *pre=[NSPredicate predicateWithFormat:@"cat_id = %@",cat_id];
    request.predicate=pre;
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"cat_id" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sort]];
    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    if (sets.count>0) {
        return  sets[0];
    }else{
        return nil;
    }

}
@end
